找传奇、传世资源到传世资源站!

C# 多线程画图 示例源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 多线程画图 示例源码 C#语言基础-第1张using System;using System.ComponentModel;using System.Drawing;using System.Threading;using System.Windows.Forms;namespace drawRegionByThread{ public partial class Form1 : Form { Thread drawThread = null; ThreadState drawThreadState = ThreadState.Unstarted; int x, y, size = 4; bool sub = false; void pDrawFuntion() { while (true) { if (ThreadState.Stopped == drawThreadState) { break; } if (ThreadState.StopRequested == drawThreadState) { drawThreadState = ThreadState.Stopped; continue; } if (ThreadState.SuspendRequested == drawThreadState) { continue; } if (ThreadState.Suspended == drawThreadState) { drawThreadState = ThreadState.Suspended; continue; } if (ThreadState.Running == drawThreadState) { lock (this) { UpdateUI(); } } } } void UpdateUI() { try { Invoke(new Action(() => { Refresh(); })); } catch { } } public Form1() { InitializeComponent(); } protected override void OnMouseDown(MouseEventArgs e) { x = e.X; y = e.Y; size = 4; } protected override void OnMouseMove(MouseEventArgs e) { x = e.X; y = e.Y; if (sub) sub = !(6 > size--); else sub = 88 < size ; } protected void drawCircle(Graphics g, int x, int y, int size = 4) { g.DrawEllipse(new Pen(new SolidBrush(Color.Lime), 2), new RectangleF(x - size, y - size, size * 2, size * 2)); } protected override void OnPaint(PaintEventArgs e) { lock (e) { drawCircle(e.Graphics, x, y, size); if (4 == size) { e.Graphics.DrawString("MouseDown", new Font("楷体", 16), new SolidBrush(Color.Red), new PointF(10, 30)); } else { e.Graphics.DrawString("MouseMove", new Font("楷体", 16), new SolidBrush(Color.Red), new PointF(10, 30)); } } } protected override void OnShown(EventArgs e) { drawThread = new Thread(pDrawFuntion); drawThread.IsBackground = true; drawThreadState = ThreadState.Running; drawThread.Start(); } protected override void OnClosing(CancelEventArgs e) { drawThreadState = ThreadState.StopRequested; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复